<template>
  <!-- <el-dialog title="添加部门" :visible="isShowDialog">
    此处应有表单
  </el-dialog> -->

  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="isShowDialog" :destroy-on-close="true" @close="$emit('update:isShowDialog', false)">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules" status-icon>
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model.trim="formData.manager" style="width:80%" placeholder="请选择" @blur="checkManager">
          <el-option
            v-for="item in employeesList"
            :key="item.id"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model.trim="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartment, addDepartment, getDepartDetail, editDepartment } from '@/api/department'
import { getEmployeesList } from '@/api/employees'
export default {
  props: {
    // eslint-disable-next-line vue/require-default-prop
    // isShow: {
    //   type: Object,
    //   required: true
    // },
    isShowDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkRepeatName = async(rule, value, callback) => {
      const { depts } = await getDepartment()
      // console.log(depts)
      // console.log(this.treeNode)
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts
        // 筛选出同一个部门的数组
          .filter(item => item.pid === this.treeNode.pid)
          // 查找部门名称与输入value值,查找到,相同返回true
          .some(item => item.name === value && item.id !== this.treeNode.id)
      } else {
        isRepeat = depts
        // 筛选出同一个部门的数组
          .filter(item => item.pid === this.treeNode.id)
          // 查找部门名称与输入value值,查找到,相同返回true
          .some(item => item.name === value)
      }
      // console.log(value)
      // console.log(isRepeat)
      if (isRepeat) {
        callback(new Error('同一个父部门下子部门必须唯一'))
      } else {
        callback()
      }
    }
    const checkRepeatCode = async(rule, value, callback) => {
      const { depts } = await getDepartment()
      let isRepeat
      if (this.formData.id) {
        // 编辑
        isRepeat = depts.some(item => item.code === value && item.id !== this.formData.id)
        isRepeat ? callback(new Error('部门编码重复了')) : callback()
      } else {
        // 新增
        isRepeat = depts.some(item => item.code === value)
        isRepeat ? callback(new Error('部门编码重复了')) : callback()
      }
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      employeesList: [],
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  async created() {
    this.employeesList = await getEmployeesList()
    // console.log(this.employeesList)
    // console.log(this.treeNode)
    // console.log(this.bool)
    // if (this.bool) {
    //   this.formData = this.treeNode
    // }
  },
  methods: {
    // 确定
    async btnOK() {
      // 新增部门
      // 对整个表单进行验证
      await this.$refs.addForm.validate()

      // console.log(this.treeNode.id)
      // console.log(this.formData)
      if (this.formData.id) {
        // 发送编辑请求
        console.log('abc', this.formData)
        const res = await editDepartment(this.formData)
        console.log(res)
        this.$message.success('编辑成功')
      } else {
        this.formData.pid = this.treeNode.id
        // 发送新增请求
        await addDepartment(this.formData)
        this.$message.success('新增成功')
      }
      // 通知父组件更新数据
      this.$emit('addDepartment')

      // 清空数据
      this.$refs.addForm.resetFields()
      // 关闭弹框
      this.$emit('update:isShowDialog', false)
    },
    // 取消
    btnCancel() {
      // 清空数据
      this.$refs.addForm.resetFields()
      // 关闭弹框
      this.$emit('update:isShowDialog', false)
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      }
      // console.log('-------------', this.formData)
    },

    // 编辑回显数据
    async getDepar(id) {
      this.formData = await getDepartDetail(id)
    },

    // 校验select选择器
    checkManager() {
      setTimeout(() => {
        // 对部分表单字段进行校验
        this.$refs.addForm.validateField('manager')
      }, 100)
    }
  }
}

</script>

<style>

</style>
